package com.denlaku.longan.controller.impl;

import cn.idev.excel.EasyExcel;
import com.denlaku.longan.controller.DataModelController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.listener.DataEventListener;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.DataModelService;
import com.denlaku.longan.vo.DataModel;
import com.denlaku.longan.vo.ExcelCell;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * @author tianx
 */
@RestController
public class DataModelControllerImpl implements DataModelController {
    @Autowired
    private DataModelService dataModelService;

    @Override
    public Return<Long> add(@RequestBody DataModel dataModel) {
        dataModelService.add(dataModel);
        return Resp.success(dataModel.getId());
    }

    @Override
    public Return<Void> update(@RequestBody DataModel dataModel) {
        dataModelService.update(dataModel);
        return Resp.success();
    }

    @Override
    public Return<Void> delete(Long id) {
        dataModelService.delete(Query.of(id));
        return Resp.success();
    }

    @Override
    public Return<DataModel> get(Long id) {
        DataModel dataModel = dataModelService.get(Query.of(id));
        return Resp.success(dataModel);
    }

    @Override
    public Return<List<DataModel>> pageList(@RequestBody DataModel dataModel, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<DataModel> list = dataModelService.list(dataModel);
        return Resp.success(list);
    }

    @Override
    @SneakyThrows
    public void template(Long id, HttpServletResponse response) {
        File file = dataModelService.template(Query.of(id));
        if (file == null) {
            response.setStatus(HttpStatus.NOT_FOUND.value());
            return;
        }
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            String fileName = "template.xlsx";
            response.setContentLengthLong(file.length());
            response.addHeader(HttpHeaders.CONTENT_DISPOSITION,
                    "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()));
            Files.copy(file.toPath(), outputStream);
            outputStream.flush();
        }
    }

    @Override
    @SneakyThrows
    public Return<Void> dataImport(Long id, MultipartRequest request) {
        dataModelService.dataImport(Query.of(id));
        MultipartFile file = request.getFile("file");
        if (file != null) {
            DataEventListener dataEventListener = new DataEventListener();
            EasyExcel.read(file.getInputStream(), null, dataEventListener).sheet().doRead();
            List<String> heads = dataEventListener.getHeads();
            Map<String, String> typeMap = dataEventListener.getTypeMap();
            List<List<ExcelCell>> rows = dataEventListener.getRows();
        }
        return Resp.success();
    }


    @Override
    public void dataExport(Long id) {
        dataModelService.dataExport(Query.of(id));
    }

}
